<template>
  <a-modal v-model:open="isShow"  @cancel="handleCancel" style="width: 1600px;">
    <a-spin :spinning="btnLoading">
      <common-table
          :columns="columns"
          :dataSource="dataList"
          :slots="['action']"
          v-model:loading="loading"
          :scroll="{ x: 'max-content'}"
      >
        <template #action="{ record }">
            <a-button type="link" @click="handleRecover(record)">
              <template #icon>
                <UndoOutlined />
              </template>
              字典取回
            </a-button>
            <a-divider type="vertical" />
            <a-popconfirm
                title="是否要彻底删除?"
                ok-text="是"
                cancel-text="否"
                @confirm="delOne(record)"
            >
              <a-button type="link">
                <template #icon>
                  <ClearOutlined />
                </template>
                彻底删除
              </a-button>
            </a-popconfirm>
        </template>
      </common-table>
    </a-spin>
    <template #footer>
      <a-button @click="handleCancel">取消</a-button>
    </template>
  </a-modal>
</template>
<script setup>
import { ref, computed,watch } from 'vue'
import {deleteDict, getBackDict, getDeleteList} from "@/api/system/dict.js";
import CommonTable from "@/components/commonTable/index.vue";
import {useSearchTable} from "@/hooks/table/useSearchTable";
import {message} from "ant-design-vue";

const { dataList, query, loading, page, search, reset, onPageChange } = useSearchTable({
  searchAPI: getDeleteList,
  queryFactory: () => ({})
})
const props = defineProps({
  openTrash: {
    type: Boolean,
    default: false
  }
})
const columns = [
  {
    title: '字典名称',
    align: "center",
    dataIndex: 'dictName'
  },
  {
    title: '字典编号',
    align: "center",
    dataIndex: 'dictCode'
  },
  {
    title: '描述',
    align: "center",
    dataIndex: 'description'
  },
  {
    title: '操作',
    dataIndex: 'action',
    align: "center",
  }
]
const emit = defineEmits(['close'])
const isShow = computed(()=>{
  return props.openTrash
})
const btnLoading = ref(false)
/**
 * 取消
 */
const handleCancel = () => {
  emit('close')
}
/**
 * 彻底删除
 * @param record
 */
const delOne = async (record) => {
  try{
    btnLoading.value = true
    const res = await deleteDict(`/sys/dict/deletePhysic/${record.id}`,{})
    message.success('删除成功')
    reset()
  }catch (e){
    message.error(e)
  }finally {
    btnLoading.value = false
  }
}
/**
 * 字典取回
 * @param record
 */
const handleRecover = async (record) => {
  try{
    btnLoading.value = true
    const res = await getBackDict({},record.id)
    message.success('字典取回成功')
    reset()
  }catch (e){
    message.error(e)
  }finally {
    btnLoading.value = false
  }
}

watch(()=>isShow.value,()=>{
  if(isShow.value){
    reset()
  }
})
</script>

<style scoped>

</style>
